﻿using Abp.AspNetCore.Mvc.Controllers;
using Batman.Cms.Core.Models.Mvc;
using Batman.Jianbei.Forum.Controllers.Users.Input;
using Batman.Jianbei.Forum.Core.Domains;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace Batman.Jianbei.Forum.Controllers.Users
{
    [Authorize]
    public class AdminController : AbpController
    {
        public UserManager<User> UserManager { get; set; }

        public SignInManager<User> _signInManager { get; set; }

        /// <summary>
        /// 管理员更新自己的密码
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> UpdatePass(UpdatePassInput model)
        {
            var userId = User.GetUserId();
            var currentUser = UserManager.Users.FirstOrDefault(t => t.Id == userId);
            //var validor = UserManager.PasswordValidators.FirstOrDefault();
            //var validResult = await validor.ValidateAsync(UserManager, currentUser, model.OldPass);

            var checkResult = await _signInManager.CheckPasswordSignInAsync(currentUser, model.OldPass, false);

            if (!checkResult.Succeeded)
                return Json(new ResponseBase(false, "旧密码错误"));

            if (model.NewPass1 != model.NewPass2)
                return Json(new ResponseBase(false, "新密码与重写密码不一致"));

            currentUser.SetPassword(model.NewPass1);

            await UserManager.UpdateAsync(currentUser);

            return Json(new ResponseBase(true));
        }
    }
}
